package com.shd.edanpei.presenter;

import android.os.CountDownTimer;
import android.text.TextUtils;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.ui.activity.LoginActivity;
import com.shd.edanpei.ui.activity.ResetLoginPwdActivity;
import com.shd.edanpei.util.MD5Util;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.Validator;
import com.shd.edanpei.enums.SMSTypeEnum;
import com.shd.edanpei.presenter.contract.ForgetPwdContract;

import java.util.WeakHashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * Created by mc on 2018/3/28.
 * yang-wen-wen
 */
public class ForgetPwdPresenter extends BasePresenterImpl<ForgetPwdContract.ForgetPwdIView>
        implements ForgetPwdContract.ForgetPwdIPresenter {

    public ForgetPwdPresenter(ForgetPwdContract.ForgetPwdIView mView) {
        super(mView);
    }

    @Override
    public void getCode(String phone, int type) {
        if (!Validator.isMobile(phone)) {
            ToastUtil.showToast("请输入正确手机号");
            return;
        }
        mBaseIView.getCodeView().setClickable(false);
        ApiCreator.getApi().sendSms(phone, type == ResetLoginPwdActivity.FORGET_PWD_LAYOUT ? SMSTypeEnum.fetchPwd.getValue() : SMSTypeEnum.resetLoginPwd.getValue(),"","")
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("短信发送成功");
                        new CountDownTimer(60 * 1000, 1 * 1000) {

                            @Override
                            public void onTick(long l) {
                                if (null != mBaseIView.getCodeView()) {
                                    mBaseIView.getCodeView().setText(l / 1000 + "s");
                                }
                            }

                            @Override
                            public void onFinish() {
                                if (null != mBaseIView.getCodeView()) {
                                    mBaseIView.getCodeView().setClickable(true);
                                    mBaseIView.getCodeView().setText("获取验证码");
                                }
                            }


                        }.start();
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getCodeView().setClickable(true);
                        mBaseIView.getCodeView().setText("获取验证码");
                    }
                });
    }


    @Override
    public void submit(final int type, String phone, String code, String password1, String password2) {
        submit(type, phone, code, null, password1, password2);
    }

    @Override
    public void submit(final int type, String phone, String code, String oldPwd, String password1, String password2) {
        //验证手机号是否合法
        if (!Validator.isMobile(phone)) {
            ToastUtil.showToast("请输入正确手机号");
            return;
        }
        //验证密码位数格式是否正确

        if (type == ResetLoginPwdActivity.UPDATA_PWD_LAYOUT && TextUtils.isEmpty(oldPwd)) {
            ToastUtil.showToast("请输入旧密码");
            return;
        }

        //验证密码是否一致
        if (!password1.equals(password2)) {
            ToastUtil.showToast("密码不一致");
            return;
        }
        if (password1.length() < 8 || password2.length() < 8) {
            ToastUtil.showToast("请设置至少8位密码");
            return;
        }
        WeakHashMap<String, Object> hashMap = new WeakHashMap<>();
        hashMap.put(ApiParams.MOBILE, phone);
        hashMap.put(ApiParams.CHECK_CODE, code);
        if (type == ResetLoginPwdActivity.UPDATA_PWD_LAYOUT && !TextUtils.isEmpty(oldPwd)) {
            hashMap.put(ApiParams.OLD_PASSWORD, MD5Util.MD5Encode(oldPwd, ""));
        }
        hashMap.put(ApiParams.PASSWORD, MD5Util.MD5Encode(password1, ""));

        ApiCreator.getApi().resetLoginPwd(hashMap)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("修改成功");
                        if (type == ResetLoginPwdActivity.FORGET_PWD_LAYOUT) {
                            mBaseIView.startActivity(LoginActivity.class);
                        }
                        mBaseIView.getContext().finish();
                    }
                });
    }
}
